package com.ls.herostory.mq;

import com.ls.herostory.thread.MyThreadFactory;
import com.rabbitmq.client.ConnectionFactory;

import java.util.concurrent.*;

/**
 * @author liangsu
 * @version v1.0
 * @Description
 * @Date 2020/10/14 16:18
 * @since
 */
public class MqConnectionFactoryUtil {

    private static ConnectionFactory connectionFactory;

    private static ConnectionFactory createConnectionFactory(){
        connectionFactory=new ConnectionFactory();
        connectionFactory.setUsername("chaoying");
        connectionFactory.setPassword("chaoying123456");
        connectionFactory.setHost("192.168.10.241");
        connectionFactory.setPort(18030);
        connectionFactory.setVirtualHost("/liangsu");

        ThreadFactory threadFactory = new MyThreadFactory("mq-startConsumer");

        ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 10,
                60, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                threadFactory,
                new RejectedExecutionHandler() {
                    @Override
                    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                        throw new RuntimeException("aaaa");
                    }
                });

        connectionFactory.setSharedExecutor(executor);
        return connectionFactory;
    }

    public static ConnectionFactory getConnectionFactory(){
        if(connectionFactory == null){
            connectionFactory = createConnectionFactory();
        }
        return connectionFactory;
    }

}
